package com.hps.spring.cloud.alibaba.controller;

import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author heps
 * @since 2020/11/3 15:02
 */
@RestController
public class NacosConsumerController {

  @Resource
  private LoadBalancerClient loadBalancerClient;

  @Resource
  private RestTemplate restTemplate;

  @Value("${spring.application.name}")
  private String appName;

  @GetMapping(value = "/test/app/name")
  public String test() {
    //使用 LoadBalanceClient 和 RestTemplate 结合的方式来访问
    ServiceInstance serviceInstance = loadBalancerClient.choose("nacos-provider");
    String url = String.format("http://%s:%s/test/%s", serviceInstance.getHost(), serviceInstance.getPort(), appName);
    return restTemplate.getForObject(url, String.class);
  }
}
